from bs4 import BeautifulSoup
from utils.re_douban import urlid_re,avatar_re


def parse_middle_str(html):
    dom = BeautifulSoup(html)
    try:
        zone = dom.find_all('span', class_="pl", text="制片国家/地区:")[0].next_sibling
    except IndexError:
        zone = None
    try:
        langue = dom.find_all('span', class_="pl", text="语言:")[0].next_sibling
    except IndexError:
        langue = None
    try:
        alias = dom.find_all('span', class_="pl", text="又名:")[0].next_sibling
    except IndexError:
        alias = None
    try:
        movie_length1 = dom.find_all('span', property="v:runtime")[0].contents[0]
    except IndexError:
        movie_length1 = None
    try:
        movie_length2 = dom.find_all('span', property="v:runtime")[0].next_sibling
    except IndexError:
        movie_length2 = None
    return {"zone": zone, "langue": langue, "alias": alias, "movie_length1": movie_length1, "movie_length2": movie_length2}


def parse_staff_str(html):
    dom = BeautifulSoup(html)
    staff_list = dom.find_all('div', class_="list-wrapper")
    """
    这一步是选出所有的类型大类的ul
    """
    all_list = dict()
    #这一层是 导演 演员 大的一层
    for staff_kind in staff_list:
        # 标识是导演还是其他
        #key = None
        tag = staff_kind.h2.next
        key=tag
        #if tag.find('导演'):
            #key = 'director'
        #if tag.find('演员'):
            #key = 'cast'
        #if tag.find("编剧"):
            #key = 'editor'
        if key:
            # 这一步是选出ul中具体的小的 li标签 里面就有详细信息 包括演员头像链接 演员的代表作 信息链接 名字
            #这一层是每层下面有很多演员
            for staffs in staff_kind.find_all('ul', class_="celebrities-list"):
                staffs_info = list()
                #这一层是单个演员信息
                for staff in staffs.find_all('li',class_='celebrity'):
                    staff_info = dict()
                    name = staff.attrs.get('title',None)
                    person_href = staff.attrs.get('href',None)
                    if person_href:
                        person_id = urlid_re(person_href)
                    else:
                        person_id = 0
                    #头像
                    img_style_url = staff.find_all('div', class_='avatar')[0].attrs['style']
                    person_avatar_url = avatar_re(img_style_url)
                    # 代表作
                    person_products = list()
                    try:
                        for product in staff.find_all('span',class_='works')[0].contents[1::2]:

                            product_name = product.get('title',None)
                            href = product.attrs.get('href',None)
                            movie_id = urlid_re(href)
                            person_products.append({'product_name':product_name,'movie_id':movie_id})
                    except IndexError:

                        pass
                    staff_info['name'] = name
                    staff_info['person_id'] = person_id
                    staff_info['products'] = person_products
                    staff_info['avatar'] = person_avatar_url
                    staffs_info.append(staff_info)
                all_list[key] = staffs_info

    return all_list
